package com.totoro.tygeneratedragonblock.executor.condition.impl;

import com.totoro.tygeneratedragonblock.constants.ConditionConst;
import com.totoro.tygeneratedragonblock.data.ConditionResult;
import com.totoro.tygeneratedragonblock.executor.condition.ConditionExecutor;
import com.totoro.tygeneratedragonblock.utils.StrUtil;
import org.bukkit.entity.Player;


public class PermissionConditionExecutor implements ConditionExecutor {
    @Override
    public ConditionResult execute(String conditionText, Player player) {
        String[] split = conditionText.split("<>");
        String failReason = null;
        if(split.length == 2) {
            conditionText = split[0];
            failReason = split[1];
        }
        ConditionResult conditionResult = new ConditionResult(false, failReason);
        conditionText = StrUtil.getPrefixAfter(conditionText, ConditionConst.PERMISSION_CONDITION);
        conditionText = getFinalConditionText(conditionText);
        conditionResult.setIsSuccess(player.hasPermission(conditionText));
        return conditionResult;
    }
}
